<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Express_model extends Base_Model
{
    const TABLE = 'hy_express';
    const PRIMAY_KEY = 'express_id';

    public function __construct()
    {
        parent::__construct();
    }

    public function getList()
    {
        
        $sql = "SELECT * FROM ".self::TABLE. " ORDER BY sort_order DESC";
        $query  = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    public function getDetail($record_id)
    {
        if (!$record_id) return [];
        $sql   = "SELECT * FROM " . self::TABLE . " WHERE express_id = {$record_id}";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function add($data)
    {
        return $this->db->insert(self::TABLE, $data);
    }

    public function edit($record_id, $data)
    {
        if (!$record_id || empty($data)) {
            return false;
        }
        $this->db->where(self::PRIMAY_KEY, $record_id);
        return $this->db->update(self::TABLE, $data);
    }

    public function  delete($record_id)
    {
        if (!$record_id) {
            return false;
        }
        $this->db->where(self::PRIMAY_KEY, $record_id);
        return $this->db->delete(self::TABLE);
    }
    public function getRecordBy($filter, $fields = " * "){
        $this->db->select($fields);
        if($name = $filter['name'] ?? ''){
            $this->db->where('name',$name);
        }
        $query = $this->db->get(self::TABLE);
        return $query->row_array();
    }
}

